package com.handpay.zztong.hp;

import java.io.File;
import com.handpay.framework.utils.CommonUtils;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.bean.ActionBar;
import com.handpay.zztong.hp.log.HPLog;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.handpay.framework.utils.BitmapLoaderNew;

/**
 * @author yanglun E-mail:sunshine_yangl@sina.com
 * @version 2015-11-24 下午2:16:50
 */

@SuppressLint("NewApi")
public class CameraCardPhotoActivity extends ZZTongActivity implements OnClickListener {
	private static final String TAG = "CardPhotoActivity";

	private ImageView photo_card_iv;
	private Button ok_btn;
	private Button take_photo_btn;
	private String path;
	private Camera camera;
	public static final int RESULT_CARAME_FAIL=-10;
	private Uri mPicUri;
	public static int TAKE_PICTURE = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_card_photo);
		super.onCreate(savedInstanceState);
		try {
			camera = Camera.open(CameraInfo.CAMERA_FACING_BACK);
		} catch (RuntimeException e) {
			Toast.makeText(this, "请打开摄像头权限", Toast.LENGTH_SHORT).show();
			setResult(CameraCardPhotoActivity.RESULT_CARAME_FAIL);
			finish();
			return ;
		}
		if(camera!=null){
			camera.release();
		}
		photo_card_iv = (ImageView) this.findViewById(R.id.photo_card_iv);
		ok_btn = (Button) this.findViewById(R.id.finshed_btn);
		take_photo_btn = (Button) this.findViewById(R.id.take_photo_btn);
		ok_btn.setOnClickListener(this);
		take_photo_btn.setOnClickListener(CommonUtils.createNoQuickResponseViewListener(3000,
				new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					try{
						File file = new File(path);
						if (file.exists()) {
							Log.i(TAG, "file del");
							file.delete();
						}
					}catch (Exception e) {
						e.printStackTrace();
					}
					Intent intent1 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
					mPicUri = CommonUtils.randomUri(CommonUtils.getAccessImgDir());
					intent1.putExtra(MediaStore.EXTRA_OUTPUT, mPicUri);
					startActivityForResult(intent1, TAKE_PICTURE);
				}
		}));
		// path = this.getIntent().getStringExtra("path");
		// BitmapLoaderNew.getInstance(this).loadImage("file://" + path,
		// photo_card_iv);
	}

	@Override
	public ActionBar getActivityActionBar() {
		return new ActionBar(this.getString(R.string.take_picture_bank_card_number), true,new OnClickListener() {
			@Override
			public void onClick(View v) {
				setResult(RESULT_CARAME_FAIL);
				finish();
			}
		});
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.finshed_btn) {
			Intent intent1 = new Intent();
			intent1.putExtra("FileUrl", path);
			this.setResult(RESULT_OK, intent1);
			this.finish();
		} 
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
//		HPLog.i(HPLog.YL_TAG, "CameraCardPhotoResult:---1");
		HPLog.i(HPLog.YL_TAG, "mPicUri:"+mPicUri);
		if (Activity.RESULT_OK == resultCode && requestCode == TAKE_PICTURE) {
//			HPLog.i(HPLog.YL_TAG, "CameraCardPhotoResult:---2");
			if (mPicUri != null) {
//				HPLog.i(HPLog.YL_TAG, "CameraCardPhotoResult:---3");
				String uri = mPicUri.toString();
				this.path = uri;
				HPLog.i(HPLog.YL_TAG, "path:"+path);

				if (uri != null && uri.length() > 0) {
//					HPLog.i(HPLog.YL_TAG, "CameraCardPhotoResult:---4");
					BitmapLoaderNew.getInstance(this).loadImage(uri, photo_card_iv);
				}
			}
		} else {
			setResult(RESULT_CARAME_FAIL);
			finish();
		}
	}
	@Override
	protected void onResume() {
		super.onResume();
		if(TextUtils.isEmpty(path)){
			//这里千万别写oncreate里，华为4.4手机会抽风
			Intent  intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
			mPicUri = CommonUtils.randomUri(CommonUtils.getAccessImgDir());
			intent.putExtra(MediaStore.EXTRA_OUTPUT, mPicUri);
			CameraCardPhotoActivity.this.startActivityForResult(intent, TAKE_PICTURE);
		}
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			setResult(RESULT_CARAME_FAIL);
			finish();
		}
		return false;
	}
}
